<!DOCTYPE html>
<html>
<head>
    <title>设置个人资料</title>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="/js/plugins/bootstrap-4.1.1-dist/css/bootstrap.min.css">
    <script src="/js/plugins/jquery/jquery.min.js"></script>
    <script src="/js/plugins/bootstrap-4.1.1-dist/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="/css/font/css/font-awesome.min.css"/>
    <link rel="stylesheet" href="/css/reset.css"/>
    <link rel="stylesheet" href="/css/myInfo.css"/>
    <script src="/js/plugins/dialog2/dialog.min.js"></script>
    <link rel="stylesheet" href="/js/plugins/dialog2/dialog.css">
    <script src="/js/plugins/jrender/jrender.min.js"></script>
    <script src="/js/plugins/form/jquery.form.js"></script>
    <script>
        $(function () {
            //获取当前登录的用户
            var user = JSON.parse(sessionStorage.getItem("user"));
            //渲染页面
            $('.info').renderValues(user);
            //渲染背景图片
            $(".myBg").css('background-image', 'url("' + user.coverImgUrl + '")');
            //设置action属性
            $('#infoForm').attr("action", "/users");
            //按下保存按钮
            $('#saveBtn').click(function () {
                $('#infoForm').ajaxSubmit(function (data) {
                    if (data.success) {
                        //保存成功，将新的个人信息保存回session中
                        sessionStorage.setItem("user", JSON.stringify(data.result));
                        //跳转到个人信息页面
                        location.href = "/mine/profiles.html";
                    } else {
                        $(document).dialog({
                            titleShow: false,
                            content: data.msg
                        });
                    }
                });
            });
            //标记当前是上传头像还是上传背景图片
            var flag;
            //点击上传头像
            $('#headBtn').click(function () {
                $('#file').click();
                flag = true;
            });
            //点击背景图案
            $('#coverBtn').click(function () {
                $('#file').click();
                flag = false;
            });
            //提交文本框
            $('#file').change(function () {
                //文本框有值才提交
                if ($(this).val()) {
                    $('#fileForm').ajaxSubmit(function (data) {
                        if (data.status == 1) {
                            //成功
                            if (flag) {
                                //上传头像
                                $('#headImg').attr('src', data.url);
                                //设置到隐藏域中
                                $('[name=headImgUrl]').val(data.url);
                            } else {
                                //上传背景
                                $(".myBg").css('background-image', 'url("' + data.url + '")');
                                //设置到隐藏域中
                                $('[name=coverImgUrl]').val(data.url);
                            }
                        }else {
                            $(document).dialog({
                                titleShow: false,
                                content: data.msg
                            });
                        }
                    });
                }
            });
        });
    </script>
</head>

<body>
    <form id="fileForm" action="/images" method="post" enctype="multipart/form-data">
        <!--隐藏的文件框-->
        <input id="file" type="file" name="file" style="display: none">
    </form>
<div class="search-head">
    <div class="row nav-search">
        <div class="col">
            <a href="javascript:window.history.go(-1);">
                <span>取消</span>
            </a>
        </div>
        <div class="col">
            <span>设置个人资料</span>
        </div>
        <div class="col">
            <a href="#" id="saveBtn">
                <span>保存</span>
            </a>
        </div>
    </div>
</div>

<div class="container info">
    <div class="myBg">
        <img id="headImg" class="rounded-circle" render-src="headImgUrl"/>
        <div class="editHead" id="headBtn"><i class="fa fa-pencil-square fa-lg " aria-hidden="true"></i></div>
        <button class="btn btn-outline-light ibtn " id="coverBtn">设置背景</button>
    </div>

    <form id="infoForm" method="post">
        <input type="hidden" name="id" render-value="id">
        <input type="hidden" name="headImgUrl" render-value="headImgUrl">
        <input type="hidden" name="coverImgUrl" render-value="coverImgUrl">
        <input type="hidden" name="email" render-value="email">
        <input type="hidden" name="_method" value="PUT">

        <label>昵称</label><input type="text" name="nickName" class="form-control" render-value="nickName">
        <label>性别</label>
        <select class="form-control" name="gender" render-value="gender">
            <option value="-1">保密</option>
            <option value="1">男</option>
            <option value="0">女</option>
        </select>
        <label>地区</label><input type="text" name="place" class="form-control" render-value="place">
        <label>签名</label><textarea name="sign" class="form-control" rows="5" render-value="sign"></textarea>
    </form>
</div>
</body>
</html>
